package com.example.reader.sdk;

import android.content.Context;
import android.util.Log;

import com.example.mtreader.MainActivity;
import com.example.reader.sdk.bean.ReaderBean;
import com.example.reader.sdk.task.ReaderTask;
import com.example.reader.sdk.utils.ThreadUtils;

import java.io.IOException;

public class DeviceReaderB extends CardReader {

    private static final String TAG = "DeviceReaderR16_801";
    private Context mContext;
    private ReaderBean mReaderBean;
    private ReaderTask mReaderTask;

    public DeviceReaderB(Context context) {
        this.mContext = context;
        this.mReaderBean = new ReaderBean();
    }

    @Override
    public void close() {

    }

    private void onBackMsg(IReaderCallback readerCallback) {
        if (readerCallback != null) {
            readerCallback.onMsg(mReaderBean);
        }
    }

    @Override
    public void init(IReaderCallback readerCallback) {
        if (mReaderTask == null) {
            mReaderTask = new ReaderTask(readerCallback, mReaderBean);
        }
        ThreadUtils.executeByFixed(1, mReaderTask);
    }

    /**
     * 关闭读卡器连接
     */
    public void closeReader() {
        if (mReaderTask != null) {
            ThreadUtils.cancel(mReaderTask);
            mReaderTask = null;
        }
        MainActivity.mt8deviceclose();
    }

    public int readId(IReaderCallback readerCallback) {
        mReaderBean.setMessage("开始读身份证");
        onBackMsg(readerCallback);
        int[] nRecLen = new int[8];
        byte[] szName = new byte[128];
        byte[] szSex = new byte[128];
        byte[] szNation = new byte[128];
        byte[] szBirth = new byte[128];
        byte[] szAddress = new byte[128];
        byte[] szIDNo = new byte[36];
        byte[] szDepartment = new byte[128];
        byte[] szDateStart = new byte[128];
        byte[] szDateEnd = new byte[128];
        byte[] szdata = new byte[3072];
        String StrWltFilePath = "";
        String StrBmpFilePath = "";
        if (MainActivity.mt8IDCardRead(szName, szSex, szNation, szBirth, szAddress, szIDNo, szDepartment, szDateStart, szDateEnd, nRecLen, szdata) != 0) {
            mReaderBean.setMessage("身份证读卡失败");
            return -100;
        }
        try {
            String name = new String(szName, "UTF-16LE");
            String idNum = new String(szIDNo, "UTF-16LE");
            mReaderBean.setName(name);
            mReaderBean.setIdNum(idNum);
//            String StrSex = getsexinfo(szSex);
//            String StrNation = getnation(szNation);
//            String StrBirth = new String(szBirth, "UTF-16LE");
//            String StrAddress = new String(szAddress, "UTF-16LE");

//            str = new String(szDepartment, "UTF-16LE");
//            str = new String(szDateStart, "UTF-16LE");
//            str = new String(szDateEnd, "UTF-16LE");
//            Log.d(TAG, str.trim());
//            Log.d(TAG, StrBirth);
//            Log.d(TAG, str);
//            StrWltFilePath = mContext.getFileStreamPath("photo.wlt").getAbsolutePath();
//            StrBmpFilePath = mContext.getFileStreamPath("photo.bmp").getAbsolutePath();
//            FileOutputStream fileOutputStream = new FileOutputStream(new File(StrWltFilePath));
//            fileOutputStream.write(szdata, 0, nRecLen[0]);
//            fileOutputStream.close();
//            if (new DecodeWlt().Wlt2Bmp(StrWltFilePath, StrBmpFilePath) == 1) {
//                Log.d(TAG, "照片解码成功");
//                return 0;
//            }
            Log.d(TAG, "照片解码失败");
            return -300;
        } catch (IOException e) {
            mReaderBean.setMessage(e.getMessage()+"-200");
            Log.d(TAG, "照片解码异常");
            return -200;
        }
    }


}
